<?php
/**
 * Created by PhpStorm.
 * User: ari
 * Date: 12/12/2014
 * Time: 5:01 PM
 */
namespace BC\Render;

use BC\PGP\KeyRoute;
use BC\SiteIndex;
use CPath\Build\IBuildable;
use CPath\Build\IBuildRequest;
use CPath\Data\Map\IKeyMap;
use CPath\Data\Map\ISequenceMap;
use CPath\Render\HTML\Element\HTMLAnchor;
use CPath\Render\HTML\HTMLContainer;
use CPath\Render\HTML\HTMLMapRenderer;
use CPath\Render\Map\MapRenderer;
use CPath\Request\IRequest;
use CPath\Route\RouteBuilder;

class CustomHTMLMapRenderer extends HTMLMapRenderer implements IBuildable
{
	const baseClass = __CLASS__;
	public function __construct(IRequest $Request, $Map) {
		parent::__construct($Request, $Map);
	}

	/**
	 * Map a value to a key in the map. If method returns true, the sequence should abort and no more values should be mapped
	 * @param String $key
	 * @param String|Array|IKeyMap|ISequenceMap $value
	 * @return bool true to stop or any other value to continue
	 */
	function map($key, $value) {
		switch ($key) {
			case 'url':
				$value = new HTMLAnchor($value, $value);
				break;
			case 'prefix':
				if (strpos($value, ' ') !== false) {
					list($method, $url) = explode(' ', $value);
					$value = new HTMLContainer(
						$method,
						new HTMLAnchor($url, $url)
					);
				}
				break;
			case 'fingerprint':
				$KeyRoute = new KeyRoute($value);
				$url      = $KeyRoute->getRouteUrl();
				$value    = new HTMLAnchor($url, preg_replace('/[^\w]/', '', $value));
				break;
		}
		return parent::map($key, $value);
	}

	/**
	 * Handle this request and render any content
	 * @param IBuildRequest $Request the build request inst for this build session
	 * @return void
	 * @build --disable 0
	 * Note: Use doctag 'build' with '--disable 1' to have this IBuildable class skipped during a build
	 */
	static function handleBuildStatic(IBuildRequest $Request) {
		$RouteBuilder = new RouteBuilder($Request, new SiteIndex(), '__map_html');
		$RouteBuilder->writeRoute('ANY *', __CLASS__);
	}
}
class_alias(CustomHTMLMapRenderer::baseClass, MapRenderer::CustomHTMLMapRenderer);

//
//namespace CPath\Render\HTML {
//	use BC\Render\CustomHTMLMapRenderer;
//
//	/** @noinspection PhpUndefinedClassInspection */
//	class HTMLMapRenderer extends CustomHTMLMapRenderer {}
//}
